package base;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import mvp.RxManager;
import utils.LogUtils;
import utils.ScreenUtil;


/**
 * Created by Mr.Q on 2018/3/10.
 * 描述
 */

public abstract class BaseDialogFragment extends DialogFragment {

    private int mWidth;
    private int mHeight;
    private int mGravity = Gravity.CENTER;

    /**
     * 自定义dialog布局文件
     * @return
     */
    protected abstract int getLayoutId();


    /**
     * 支持对Rx操作
     */
    protected RxManager mRxManager = new RxManager();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        LogUtils.logd("onCreateView~~~~~");

        Window window = getDialog().getWindow();
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(getLayoutId(), ((ViewGroup) window.findViewById(android.R.id.content)), false);//需要用android.R.id.content这个view
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//设置背景颜色,只有设置了这个属性,宽度才能全屏MATCH_PARENT
        mWidth = (int) (mWidthScale * ScreenUtil.getScreenWidth(getContext()));
        mHeight = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setLayout(mWidth, mHeight);//这2行,和上面的一样,注意顺序就行;
        return view;
    }

    @Override
    public int show(FragmentTransaction transaction, String tag) {
        LogUtils.logd("show~~~~~1");
        return super.show(transaction, tag);
    }

    @Override
    public void show(FragmentManager manager, String tag) {
        LogUtils.logd("show~~~~~2");
        super.show(manager, tag);
    }

    @Override
    public void onResume() {
        super.onResume();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = mGravity;
        wlp.width = mWidth;
        wlp.height = mHeight;
        window.setAttributes(wlp);
    }
    /**
     * 设置弹窗宽度相对屏幕宽度显示的比例
     * @param widthScale
     */
    public void setWidthScale(float widthScale) {
        mWidthScale = widthScale;
        mWidth = (int) (mWidthScale * ScreenUtil.getScreenWidth(getContext()));
    }

    private float mWidthScale = 0.6f;

    public void setGravity(int gravity) {
        mGravity = gravity;
    }

    public void setWidth(int width) {
        mWidth = width;
    }
    public void setHeight(int height) {
        mHeight = height;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mRxManager.clear();
    }
}


